﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using DemoManageSys.Areas.Filters;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;

namespace DemoManageSys.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class CustomerAddressController : Controller
    {
        private ICustomerAddressBLL _customerAddressBLL;

        public CustomerAddressController(
            ICustomerAddressBLL customerAddressBLL
            )
        {
            _customerAddressBLL = customerAddressBLL;
        }

        #region 显示视图
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateView()
        {
            return View();
        }

        public IActionResult EditView()
        {
            return View();
        }
        #endregion

        #region 获取顾客地址表的方法 (GetCustomerAddresses)
        /// <summary>
        /// 获取顾客地址表的方法
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCustomerAddress(int limit, int page, string customerAddressName)
        {
            int count;
            List<GetCustomerAddressDTO> getCustomerAddressDTOs = _customerAddressBLL.GetCustomerAddress(limit, page, customerAddressName, out count);
            ReturnResule resule = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getCustomerAddressDTOs,
                Count = count
            };
            return Json(resule);
        }
        #endregion

        #region 添加的方法 (CreateCustomerAddress)
        /// <summary>
        /// 添加的方法
        /// </summary>
        /// <param name="cate"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateCustomerAddressH([FromForm] CustomerAddress cate)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            string msg;//定义返回消息
            resule.IsSuccess = _customerAddressBLL.CreateCustomerAddressH(cate, out msg);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }
            resule.Msg = msg;

            return Json(resule);
        }
        #endregion

        #region 删除的方法 (DeleteCustomerAddress)
        /// <summary>
        /// 删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteCustomerAddress(string id)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "删除的id不能为空";
                return Json(resule);
            }

            resule.IsSuccess = _customerAddressBLL.DeleteCustomerAddress(id);//调用方法传值

            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }
            resule.Msg = resule.IsSuccess ? "删除地址成功" : "删除地址失败";

            return Json(resule);
        }
        #endregion

        #region 批量软删除地址的方法 (DeleteListCustomerAddresso)
        /// <summary>
        /// 批量软删除地址的方法
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteListCustomerAddresso(List<string> ids)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (ids.Count == 0)
            {
                resule.Msg = "你没有返回任何id";
                return Json(resule);
            }
            resule.IsSuccess = _customerAddressBLL.DeleteListCustomerAddress(ids);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }
            resule.Msg = resule.IsSuccess ? "删除地址成功" : "删除地址失败";

            return Json(resule);
        }
        #endregion

        #region 修改的方法 (UpdateCustomerAddress)
        /// <summary>
        /// 修改的方法
        /// </summary>
        /// <param name="cate"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCustomerAddress([FromForm] CustomerAddress cate)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();

            string msg;//定义返回消息
            resule.IsSuccess = _customerAddressBLL.UpdateCustomerAddress(cate, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion
    }
}